package com.team2.outpatient_sys.service.impl;

import com.team2.outpatient_sys.dao.DoctorDao;
import com.team2.outpatient_sys.dao.PatientDao;
import com.github.pagehelper.PageHelper;
import com.team2.outpatient_sys.dao.PrescriptionDao;
import com.team2.outpatient_sys.pojo.Doctor;
import com.team2.outpatient_sys.pojo.Patient;
import com.team2.outpatient_sys.pojo.Prescription;
import com.team2.outpatient_sys.pojo.PrescriptionAndName;
import com.team2.outpatient_sys.service.PrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PrescriptionServiceImpl implements PrescriptionService {
    @Autowired
    private PrescriptionDao prescriptionDao;
    @Autowired
    private DoctorDao doctorDao;
    @Autowired
    private PatientDao patientDao;

    @Override
    public int add(Prescription prescription) {
        return prescriptionDao.add(prescription);
    }

    @Override
    public List<Prescription> findAllPrescription() {
        return prescriptionDao.findAllPrescription();
    }

    @Override
    public List<Prescription> findByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Prescription> list=prescriptionDao.findAllPrescription();
        return list;
    }

    @Override
    public List<PrescriptionAndName> getAndDoctorNamePatientName() {
        // 查询所有处方信息
        List<PrescriptionAndName> andPatientDoctorNameList = prescriptionDao.getAndPatientDoctorName();

        //遍历处方信息，存入开方人姓名和病人姓名
        for (PrescriptionAndName prescriptionAndName : andPatientDoctorNameList) {
            Doctor doctor = doctorDao.queryById(prescriptionAndName.getDoctorId());
            prescriptionAndName.setDoctorName(doctor.getName());
            Patient patient = patientDao.findById(prescriptionAndName.getPatientId());
            prescriptionAndName.setPatientName(patient.getName());
        }

        return andPatientDoctorNameList;
    }

    @Override
    public List<PrescriptionAndName> getAndDoctorNamePatientNameByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        // 查询所有处方信息
        List<PrescriptionAndName> andPatientDoctorNameList = prescriptionDao.getAndPatientDoctorName();

        //遍历处方信息，存入开方人姓名和病人姓名
        for (PrescriptionAndName prescriptionAndName : andPatientDoctorNameList) {
            Doctor doctor = doctorDao.queryById(prescriptionAndName.getDoctorId());
            prescriptionAndName.setDoctorName(doctor.getName());
            Patient patient = patientDao.findById(prescriptionAndName.getPatientId());
            prescriptionAndName.setPatientName(patient.getName());
        }

        return andPatientDoctorNameList;
    }
}
